package com.jcl.recruit.user.mapper;

import com.jcl.recruit.user.model.UserExternalAccess;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserExternalAccessMapper {

    @Select({
            "<script>",
            "select * from user_external_access ",
            "<where>",
            "<if test=\"null != category\"> and category = #{category} </if>",
            "<if test=\"null != statusList\"> and status in ",
            "<foreach collection=\"statusList\" item=\"status\" open=\"(\" close=\")\" separator=\",\"> " ,
            " #{status}",
            "</foreach>",
            " </if>",
            "</where>",
            "</script>"
    })
    List<UserExternalAccess> findList(@Param("category") String category, @Param("statusList") Integer... status);

    @Select({
            "<script>",
            "select * from user_external_access ",
            "<where>",
            "<if test=\"null != externalKey\"> external_key = #{externalKey} </if>",
            "<if test=\"null != category\"> and category = #{category} </if>",
            "<if test=\"null != status\"> and status = #{status} </if>",
            "</where>",
            "</script>"
    })
    List<UserExternalAccess> findListByExternalKey(@Param("externalKey") String externalKey,
                                                   @Param("category") String category,
                                                   @Param("status") Integer status);

    @Select({
            "<script>",
            "select * from user_external_access ",
            "<where>",
            "<if test=\"null != username\"> username = #{username} </if>",
            "<if test=\"null != category\"> and category = #{category} </if>",
            "<if test=\"null != status\"> and status = #{status} </if>",
            "</where>",
            "</script>"
    })
    List<UserExternalAccess> findListByUsername(@Param("username") String username,
                                                @Param("category") String category,
                                                @Param("status") Integer status);

    @Select({
            "select * from user_external_access where external_key = #{externalKey} and corp_code = #{corpCode}"
    })
    UserExternalAccess findOneByExternalKeyAndCode(@Param("externalKey") String externalKey,
                                                   @Param("corpCode") String corpCode);

    @Select({
            "select * from user_external_access where user_id = #{userId} and category = #{category}"
    })
    UserExternalAccess findOneByUserId(@Param("userId") Integer userId, @Param("category") String category);

    @Select({
            "select * from user_external_access where id = #{id}"
    })
    UserExternalAccess findOne(@Param("id") Integer id);


    @InsertProvider(type= UserExternalAccessProvider.class, method = "insertProvider")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int add(UserExternalAccess access);

    @UpdateProvider(type= UserExternalAccessProvider.class, method = "updateProvider")
    int updateAccess(UserExternalAccess access);

    @Update({
            "update user_external_access set user_id = #{userId} where external_key = #{externalKey} and category = #{category}"
    })
    int update(@Param("externalKey") String externalKey,
               @Param("userId") Integer userId,
               @Param("category") String category);

    @Update({
            "<script>",
            "update user_external_access set status = #{setting} ",
            "<where>",
            "<if test=\"null != externalKey\"> and external_key = #{externalKey} </if>",
            "<if test=\"null != category\"> and category = #{category} </if>",
            "<if test=\"null != statuses\"> and status in ",
            "<foreach collection=\"statuses\" item=\"status\" open=\"(\" close=\")\" separator=\",\"> " ,
            " #{status}",
            "</foreach>",
            " </if>",
            "</where>",
            "</script>"
    })
    int changeStatusesToSetting(@Param("setting") Integer setting,
                                @Param("externalKey") String externalKey,
                                @Param("category") String category,
                                @Param("statuses") Integer... statuses);

}
